<div id="main">
    <div id="contents">
        <form method="post" action="#" name="filters">
            <fieldset>
                <legend>
                    <label sid="trunk.stat"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" cellspacing="0" cellpadding="0" border="0" width="100%">
                        <tbody>
                        <tr id="tr478">
                            <td class="frame_tab_left">
                                <span id="img478" class="errormsg">&nbsp;</span>
                                <label for="id478">
                                    <label sid="sip_trunk.table1_td2"></label>
                                </label>
                            </td>
                            <td id="td478" class="frame_tab_right">
                                <input id="id478" type="text" name="id478">
                                <span id="msg478">&nbsp;</span>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2"></td>
                        </tr>
                        <tr>
                            <td class="frame_tab_center" colspan="2">
                                <input class="button" type="button" name="submit" sid="sbutton.save">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <fieldset>
                <legend>
                    <label sid="trunk.add"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" cellspacing="0" cellpadding="0" border="0" width="100%">
                        <tbody>
                        <tr>
                            <td class="frame_tab_left">
                                <span id="img401" class="errormsg">&nbsp;</span>
                                <label for="id401">
                                    <label sid="trunk.ID"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id401" type="text" name="id401">
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <span id="img432" class="errormsg">&nbsp;</span>
                                <label for="id432">
                                    <label sid="trunk.password"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id432" type="password" value="" name="id432">
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="id433">
                                    <label sid="trunk.registration"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id433" type="checkbox" name="id433">
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="id433">
                                    <label sid="trunk.usertype"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <select id="line_type" onchange="readtype();" name="line_type">
                                    <option selected="" value="0" sid="trunk.usertype1"></option>
                                    <option value="1" sid="trunk.usertype2"></option>
                                </select>
                            </td>
                        </tr>
                        <tr id="tr469" style="display:none;">
                            <td class="frame_tab_left">
                                <span id="img469" class="errormsg">&nbsp;</span>
                                <label for="id469">
                                    <label sid="trunk.DID"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id469" type="text" onblur="read469();" name="id469">
                            </td>
                        </tr>
                        <tr id="tr468">
                            <td class="frame_tab_left">
                                <label for="id468">
                                    <label sid="trunk.greeting"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <select id="id468" name="id468">
                                    <option value="" sid="trunk.attendent"></option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2"></td>
                        </tr>
                        <tr>
                            <td class="frame_tab_center" colspan="2">
                                <input id="save_button" class="button" type="button" onclick="virtual_add();" name="save_button" sid="sbutton.add">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <fieldset>
                <legend>
                    <label sid="trunk.currents"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table id="iptrunk" class="cmenutitle" cellspacing="0" cellpadding="0" border="0" width="100%"> </table>
                </div>
            </fieldset>
        </form>
    </div>
</div>
<br>
<div id="helpContainer">
    <div id="help">
        <div>
            <h2>
                <label sid="share.help"></label>
            </h2>
        </div>
        <div id="helpBody">
            <dl>

                <dt class="term">
                    <label sid="trunk.stat_1"></label>
                </dt>
                <dd class="definition">
                    <label sid="trunk.stat_1Tip"></label>
                </dd>
                <dt class="term">
                    <label sid="trunk.usertype"></label>
                </dt>
                <dd class="definition">
                    <label sid="trunk.usertype1"></label><br>
                    <label sid="trunk.usertype1_note"></label><br><br>
                    <label sid="trunk.usertype2"></label><br>
                    <label sid="trunk.usertype2_note"></label>
                </dd>

            </dl>

        </div>
    </div>
</div>

<script type="text/javascript" src="js/libTable.js"></script>
<script type="text/javascript">
$(document).ready(function(){

    load_html();

});

var virtual_data = new Array();
var old_virtual=0;
var total = 0;
var nullnum =0;
var maxtotal = 0;
var alllist = new Array();
function load_html(){
	if(lineshow("4001")){
	var groupid="&id=4021&id=771&id=478";
	var ivrList='<option value="">'+trunk.attendent+'</option>';
	xmlDataRequest("get_data",groupid,function(data){
			var sendtext = ''; var flag=0;
			$(data).find("item").each(function(){
			if($(this).attr("id")=="4021"){
				var s=0;
  			$(this).find("value").each(function(){
  				if($.trim($(this).text().split("/")[2])!==""){
  					sendtext += "&line_id="+$.trim($(this).text().split("/")[1]);
  					flag++;
  					total =$.trim($(this).text().split("/")[1]);
  				}else{
  					nullnum=$.trim($(this).text().split("/")[1]);
  					}
  				alllist[s]=$.trim($(this).text().split("/")[1]);
  				s++;
  				maxtotal=$.trim($(this).text().split("/")[1]);
  			});
  			if(total==0){
  				total=parseInt(alllist[0])-1;
  				}
  		}else if($(this).attr("id")=="771"){
  			var listarray = $(this).attr("value").split("|");
  			var ring = new Array();
  			var n=0;
  			for(var s = 0; s < listarray.length; s++){
  				if(listarray[s].indexOf("user")>=0||listarray[s].indexOf("welcome")>=0){
  			    	 ring[n] = listarray[s].split(".")[0];
  			    	 n++;
  			    }
  			}
  			var truearray = unique(ring);
  			for(var i =0 ;i <truearray.length; i++){
  				ivrList += '<option>'+truearray[i]+'</option>';
  			}
  		 $("#id468").html(ivrList);
  		}else if($(this).attr("id")=="478"){
  			var value478 = $(this).attr("value");
  		 $("#id478").attr("value",value478);
  		}
  		});
  			if(sendtext!==""){
			DataRequest("get_json",sendtext,function(data){
				virtual_paint_list(data,flag);
				virtual_data = data;
			});
		}
});
}
}

function virtual_paint_list(data,flag){
	if(flag!==0&&flag!==1){
 var data_new=new Array();
 var j=0;
  for(var i=0;i<data.length;i++){
  var line_id = data[i].line_id;
  var id401 = "["+line_id+"]"+replacestr(data[i].id401);
  var id433 = data[i].id433;
  var id432 = replacestr(data[i].id432);
  var id469 = replacestr(data[i].id469);
  var id468 = replacestr( data[i].id468);
  var id430 = data[i].id430;
 if(id430=="SUCCESS"){
  var re_stat = "["+id433+"]img_reok|"+trunk.reg_stat1;
}else if(id430=="FAIL"){
	var re_stat = "["+id433+"]img_refail|"+trunk.reg_stat2;
}else if(id430=="N/A"){
	var re_stat = "["+id433+"]img_nore|"+trunk.reg_stat3;
}else if(id430=="TIMEOUT"){
	var re_stat = "["+id433+"]img_refail|"+trunk.reg_stat4;
}else{
	var re_stat = "["+id433+"]img_refail|"+trunk.reg_stat2;
	}
   var linetype='';

      if (id469 !== "") {
          linetype = "[" + id469 + "]" + trunk.usertype2 + "(" + id469 + ")";
      }
      if (id468 !== "") {
          linetype = "[" + id468 + "]" + trunk.usertype1;
      }

      if(id469==""&&id468==""){
          linetype = "[" + id468 + "]" + trunk.usertype1;
      }

      data_new.push({id401:id401,id432:id432,linetype:linetype,re_stat:re_stat});
  j = i;
 }
 var fun_arr = [{type:"modify",name:"virtual_change"},{type:"del",name:"virtual_del"}];
 var tab = new libTable(new Array(trunk.ID,trunk.password,trunk.usertype,trunk.reg_stat,msg.op),data_new,1,10);
 print_table("iptrunk",tab,fun_arr, true);
}else{
	 var data_new=new Array();
  var line_id = data.line_id;
  var id401 = "["+line_id+"]"+replacestr(data.id401);
  var id433 = data.id433;
  var id432 = replacestr(data.id432);
  var id469 = replacestr(data.id469);
  var id468 = replacestr( data.id468);
   var id430 = data.id430;
   if(id430=="SUCCESS"){
  var re_stat = "["+id433+"]img_reok|"+trunk.reg_stat1;
}else if(id430=="FAIL"){
	var re_stat = "["+id433+"]img_refail|"+trunk.reg_stat2;
}else if(id430=="N/A"){
	var re_stat = "["+id433+"]img_nore|"+trunk.reg_stat3;
}else if(id430=="TIMEOUT"){
	var re_stat = "["+id433+"]img_refail|"+trunk.reg_stat4;
}else{
	var re_stat = "["+id433+"]img_refail|"+trunk.reg_stat2;
	}
        var linetype = '';
        if (id469 !== "") {
            linetype = "[" + id469 + "]" + trunk.usertype2 + "(" + id469 + ")";
        }
        if (id468 !== "") {
            linetype = "[" + id468 + "]" + trunk.usertype1;
        }

        if(id469==""&&id468==""){
            linetype = "[" + id468 + "]" + trunk.usertype1;
        }
  data_new.push({id401:id401,id432:id432,linetype:linetype,re_stat:re_stat});
 var fun_arr = [{type:"modify",name:"virtual_change"},{type:"del",name:"virtual_del"}];
 var tab = new libTable(new Array(trunk.ID,trunk.password,trunk.usertype,trunk.reg_stat,msg.op),data_new,1,10);
 print_table("iptrunk",tab,fun_arr, true);
}
}

function virtual_change(id,id401,id432,id468,linetype,id433,re_stat)
{
 old_virtual = parseInt(id);
  $("#id401").val(id401);
  $("#id432").val(id432);
   if(id433 == "on"){
   	$("#id433").attr("checked",true);
  }else{
  	$("#id433").attr("checked",false);
  	}
  if(linetype.indexOf(trunk.usertype1)>=0){
   listselect("line_type",0);
   readtype();
   listselect("id468",id468);
  }else if(linetype.indexOf(trunk.usertype2)>=0){
 	 listselect("line_type",1);
 	 readtype();
 	 $("#id469").val(id468);
  }
 Modify_button();
}

function virtual_add(){
  var id401 =$("#id401").val();
  if($("#id433").attr("checked")){
  var id433 = "on";
}else{
	var id433 = "off";
}
  var id432 = $("#id432").val();
  var id469 = $("#id469").val();
  var id468 = $("#id468").val();
    if($("#line_type").val()==1){
        id468 ="";
    }
  var settext='';
  if($("#save_button").val()==sbutton.modify){
	settext+='&line_id='+old_virtual;
  }else if(nullnum!=0){
  	settext+='&line_id='+nullnum;
  	$("#save_button").val(sbutton.add);
  }else if(total < maxtotal){
  	settext+='&line_id='+parseInt(parseInt(total)+parseInt(1));
  	$("#save_button").val(sbutton.add);
  }
    settext+="&id401="+id401;
  if(settext.indexOf("&line_id=")>=0){
  if(id469!==""){
			settext+="&id437=on&id447=on&id469="+$("#id469").val();
			}else{
				settext+="&id437=off&id447=off&id469="+$("#id469").val();
			}
 settext+="&id432="+id432+"&id433="+id433+"&id468="+id468;
  submitform(modules["set_data"],settext,"ip_trunk_main_load");
}else{
	alert(msg.lineLimit);
	}
//  load_html();
}

function virtual_del(id,id401,id432,id468,linetype,id433,re_stat){
 if(confirm(msg.confirm_del)){
// show_message("del",msg.deleting);
  var settext='&line_id='+id+"&id401=&id432=&id433=off&id469=&id437=off&id447=off&id468=";
  submitform(modules["set_data"],settext,"ip_trunk_main_load");
 // load_html();
 }
}

	$("[name=submit]").click(function () {
		$(".errormsg").html('');
		var settext="&id478="+$("#id478").val()+"&id477="+$("#id478").val();
		submitform(modules["set_data"],settext,"ip_trunk_main_load");
	});
	
	function readtype(){
		if($("#line_type").val()==1){
				$("#tr469").show();
				$("#tr468").hide();
                $("#id468").val("");
			}else if($("#line_type").val()==0){
				$("#tr469").hide();
                $("#id469").val("");
				$("#tr468").show();
				}else{
					$("#tr469").hide();
				    $("#tr468").show();
					}
		}
	
		function read469(){
		if($("#id469").val()!=""){
			$("#s_447_1").html(trunk.outbound_option4);
			$("#s_447_2").html(trunk.outbound_option3);
			$("#id447_2").attr("checked",true);
		}else{
			$("#s_447_1").html(trunk.outbound_option1);
			$("#s_447_2").html(trunk.outbound_option2);
		}
		}
		function read(){
		if($("#id469").val()!=""){
			$("#s_447_1").html(trunk.outbound_option4);
			$("#s_447_2").html(trunk.outbound_option3);
		}else{
			$("#s_447_1").html(trunk.outbound_option1);
			$("#s_447_2").html(trunk.outbound_option2);
		}
	}
</script>

